﻿using JumpstartApp.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Reactive;
using System.Reactive.Linq;

// The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=234227

namespace JumpstartApp
{
    sealed partial class App : Application
    {
        public const string AppNameForSkyDrive = "Windows 8 Jumpstart ( DPEUSW )";

        public App()
        {
            this.InitializeComponent();
            this.Suspending += (s, e) => Services.Navigation.SaveState();
        }

        protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            if (args.TileId != "App" && args.PreviousExecutionState == ApplicationExecutionState.Running)
            {
                Services.Navigation.GotoDetail(args.TileId);
            }
            else
            {
                Window.Current.Content = new Views.Splash(args);
                Activate();
            }
        }

        protected override void OnActivated(IActivatedEventArgs args)
        {
            if (args.Kind == ActivationKind.Protocol)
            {
                Window.Current.Content = new Views.Splash(args as ProtocolActivatedEventArgs);
                Activate();
            }
            else
            {
                Services.Navigation.GotoFail(string.Format("Unsupported activation type: {0}", args.Kind));
            }
        }

        protected override void OnSearchActivated(SearchActivatedEventArgs args)
        {
            Window.Current.Content = new Views.Splash(args);
            Activate();
        }

        void Activate()
        {
            // this delay allows the ext splash image to load and render
            var _Timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1000) };
            _Timer.Tick += (s, e) =>
            {
                _Timer.Stop();
                Window.Current.Activate();
            };
            _Timer.Start();
        }
    }
}
